
class Solution {
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        if(n == 1)
            return nums[0];
        int sum_num = nums[0];
        int[] dp_max = new int[n];
        dp_max[0] = nums[0];
        int[] dp_min = new int[n];
        dp_min[0] = nums[0];
        int max_dp = nums[0], min_dp = nums[0];
        for(int i = 1; i < n; i ++)
        {
            dp_max[i] = Math.max(dp_max[i - 1] + nums[i], nums[i]);
            max_dp = Math.max(max_dp, dp_max[i]);
            dp_min[i] = Math.min(dp_min[i - 1] + nums[i], nums[i]);
            min_dp = Math.min(min_dp, dp_min[i]);
            sum_num += nums[i];
        }
        int max1 = max_dp;
        int max2 = sum_num - min_dp;
        if(max2 == 0)
            return max1;
        return Math.max(max1, max2);
    }
}

